﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Reflection;
using System.Web;
using He.Common;
using System.IO;

namespace He.Web.UI
{
    public class MemberHelper
    {
        /// <summary>
        /// 通过枚举返回会员状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string GetUserStatus(object status)
        {
            Type typeDescription = typeof(DescriptionAttribute);
            System.Reflection.FieldInfo[] fields = typeof(Enums.MemberStatus).GetFields();
            foreach (FieldInfo field in fields)
            {
                if (field.IsSpecialName) continue;
                if (field.GetRawConstantValue().ToString() == status.ToString())
                {
                    return field.Name;
                }
            }
            return string.Empty;
        }
        /// <summary>
        /// 判断会员是否登录
        /// </summary>
        /// <returns></returns>
        public static bool IsLogin()
        {
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 替换注册邮件模板
        /// </summary>
        /// <param name="emailBody"></param>
        /// <param name="member"></param>
        /// <param name="random_code"></param>
        /// <returns></returns>
        public static string ReplaceTemplate(string emailBody, Model.member member)
        {
            Model.site_info SiteInfo = new BLL.site_info().GetModelByLang("cn");
            Model.webset SiteConfig = new BLL.webset().GetWebset();

            return emailBody.Replace("{user_email}", member.email).Replace("{web_name}", SiteInfo.WebName).Replace("{jump_url}", string.Format("<a href='http://{0}/user/reg_activate.aspx?uid={1}&rid={2}'>http://{0}/user/reg_activate.aspx?uid={1}&rid={2}</a>", SiteConfig.site_domain.Trim('/'), member.id, member.reset_key)).Replace("{send_ip}", HttpContext.Current.Request.UserHostAddress).Replace("{web_url}", string.Format("<a href='http://{0}'>http://{0}</a>", SiteConfig.site_domain));
        }

        /// <summary>
        /// 读取邮件模板
        /// </summary>
        /// <param name="email_path"></param>
        /// <returns></returns>
        public static string ReadTemplate(string email_path)
        {
            FileStream fs = new FileStream(PathHelper.MapPath(email_path), FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            string str = sr.ReadToEnd();
            fs.Dispose();
            sr.Dispose();
            return str;
        }

        /// <summary>
        /// 获取会员等级
        /// </summary>
        /// <param name="rankId"></param>
        /// <returns></returns>
        public static string GetMemberRank(int rankId)
        {
            Model.member_rank model = new BLL.member_rank().GetModel(rankId);
            if (model != null)
            {
                return model.title;
            }
            return string.Empty;
        }
    }
}
